package com.shy.collect.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shy.collect.entity.UserAccount;
import com.shy.collect.entity.UserInfo;
import com.shy.collect.mapper.UserAccountMapper;
import com.shy.collect.mapper.UserInfoMapper;
import com.shy.collect.service.UserService;
import com.shy.configs.common.enums.PublicStatus;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.common.exceptions.DftException;
import com.shy.configs.common.utils.IdUtil;
import com.shy.configs.mvc.models.query.UserEditQuery;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper accountMapper;

    /**
     * 分页查询用户列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<UserInfo> pageList(Integer pageNo, Integer pageSize) {

        return userInfoMapper.selectPage(Page.of(pageNo, pageSize),
                new QueryWrapper<UserInfo>().orderByDesc("create_time"));
    }

    /**
     * 根据id查询用户信息
     *
     * @param userNo
     * @return
     */
    @Override
    public UserInfo getUserByUserNo(String userNo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no", userNo);
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public void editUser(UserEditQuery editQuery) {
        // 修改
        if (StrUtil.isNotBlank(editQuery.getId())) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(editQuery.getId());
            userInfo.setLoginName(editQuery.getLoginName());
            userInfo.setPhone(editQuery.getPhone());
            userInfo.setShopName(editQuery.getShopName());
            userInfo.setShopScope(editQuery.getShopScope());
            userInfo.setUserEmail(editQuery.getUserEmail());
            int count = userInfoMapper.updateById(userInfo);
            if (count == 0) {
                throw new DftException(RCode.FAIL);
            }
        } else {
            // 新增
            //业务要求 login_name不能重复
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name", editQuery.getLoginName());
            Long count = userInfoMapper.selectCount(wrapper);
            if (count > 0) {
                throw new DftException(RCode.FAIL_USER_EXIST);
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setId(IdUtil.uuid());
            userInfo.setUserNo(IdUtil.userNo());
            userInfo.setLoginName(editQuery.getLoginName());
            userInfo.setAccountNo(IdUtil.accountNo());
            userInfo.setPhone(editQuery.getPhone());
            userInfo.setPassword(IdUtil.uuid());
            userInfo.setPayPwd(IdUtil.uuid());
            userInfo.setShopName(editQuery.getShopName());
            userInfo.setShopScope(editQuery.getShopScope());
            userInfo.setUserEmail(editQuery.getUserEmail());
            userInfo.setStatus(PublicStatus.ENABLE.name());
            userInfo.setCreateTime(new Date());
            userInfo.setRemark("创建商户");
            userInfoMapper.insert(userInfo);

            UserAccount account = new UserAccount();
            account.setId(IdUtil.uuid());
            account.setUserNo(userInfo.getUserNo());
            account.setAccountNo(userInfo.getAccountNo());
            account.setBalance(new BigDecimal("0"));
            account.setTotalIncome(new BigDecimal("0"));
            account.setTotalExpend(new BigDecimal("0"));
            account.setStatus(PublicStatus.ENABLE.name());
            account.setRemark("创建资金账户");
            account.setEditTime(new Date());
            accountMapper.insert(account);
        }
    }

    @Override
    public List<UserInfo> likeShopNameList(String shopName) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.like("shop_name", shopName).orderByAsc("shop_name").last("limit 0, 50");
        return userInfoMapper.selectList(wrapper);
    }
}
